package com.ajaaxx.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ajaaxx.datatypes.Prototype;
import com.ajaaxx.datatypes.Question;
import com.ajaaxx.datatypes.Subject;
import com.ajaaxx.datatypes.SurveyForm;
import com.ajaaxx.datatypes.User;
import com.ajaaxx.service.EvaluationService;


@Controller
@RequestMapping("/spring")
public class MVCController {
	
	@Autowired
	private EvaluationService evaluationService;
	
	
	@RequestMapping(value="/startSurvey",method=RequestMethod.GET)
	public String invoke(@PathVariable(value="cedulla") String cedulla){
		System.out.println(cedulla);
		return "second";
	}
	
	@RequestMapping(value="/fetchUserDetails",method=RequestMethod.POST)
	public @ResponseBody User fetchUserDetails(@RequestBody final User user){
		
		System.out.println("fetchUserDetails is being called : "+user.getCedulla());
		User result = evaluationService.getUserBasedOnCedulla(user.getCedulla());
		if(result==null){return new User();}
		return result;
	}
	
	
	
	@RequestMapping(value="/fetchQuestions",method=RequestMethod.POST)
	public @ResponseBody List<Question> fetchQuestions(@RequestBody final Prototype prototype){
		
		System.out.println("fetchQuestions is being called : "+prototype.getCedulla());
		Set<Question> questions = evaluationService.getQuestions(prototype.getCedulla(), prototype.getSubject(), prototype.getCurrentSemester(), prototype.getUserType());
		List<Question> result = new ArrayList<Question>();
		for(Question q:questions){
			result.add(q);
		}
		
		return result;
	}
	
	
	
	/*@SuppressWarnings("unchecked")
	@RequestMapping(value="/survey/{cedulla}")
	public ModelAndView startSurvey(@ModelAttribute("form") SurveyForm form,@PathVariable("cedulla") String cedulla,HttpSession session,ModelMap modelMap){
		
		
		System.out.println("Starting survey of "+cedulla);
		
				
		String ongoingSurvey= (String)session.getAttribute("ONGOING_SURVEY");
		Set<Subject> addedSubjects = (Set<Subject>)session.getAttribute("SUBJECTS");
		
		
		if(ongoingSurvey==null||ongoingSurvey.equals("")){
			
			User user = evaluationService.getUserBasedOnCedulla(Integer.parseInt(cedulla));			
			Set<Subject> subjectsToAdd = evaluationService.getSubjects(user);
			
			Subject currentSubject = null;			
			Set<Question> questions = null;
			
			for(Subject subject:subjectsToAdd){
				currentSubject = subject;
				break;
			}
			
			subjectsToAdd.remove(currentSubject);
			
			questions = evaluationService.getQuestions(user.getCedulla(), currentSubject.getSubjectId(), user.getCurrentSemester(),user.getUserType());
			
			
			// Setting attributes in the session
			session.setAttribute("USER",user);
			session.setAttribute("SUBJECTS", subjectsToAdd);
			session.setAttribute("ONGOING_SURVEY", "TRUE");
			session.setAttribute("QUESTIONS", new ArrayList<Question>());
			
			return new ModelAndView("survey","form",createForm(form,user, currentSubject, questions));
			
		}
		
		
		
		
		else if(!addedSubjects.isEmpty() & ongoingSurvey!=null & ongoingSurvey.equals("TRUE")){
			
			User user = ((User)session.getAttribute("USER"));
			List<Question> addedQuestions = (ArrayList<Question>)session.getAttribute("QUESTIONS");
			for(Question q:form.getQuestions()){
				addedQuestions.add(q);
			}
			
			
			
			Set<Subject> remainingSubjects = (Set<Subject>)session.getAttribute("SUBJECTS");
			Subject currentSubject = null;
			for(Subject subject:remainingSubjects){
				currentSubject = subject;
				break;
			}
			
			remainingSubjects.remove(currentSubject);
			
			Set<Question> questions = evaluationService.getQuestions(user.getCedulla(), currentSubject.getSubjectId(), user.getCurrentSemester(),user.getUserType());
			
		
			return new ModelAndView("survey","form", createForm(form,user, currentSubject, questions));
			
		}
		
		
		
		
		
		else if(addedSubjects.isEmpty() & ongoingSurvey!=null & ongoingSurvey.equals("TRUE")){
			
			User userObject = (User)session.getAttribute("USER");
			List<Question> addedQuestions = (ArrayList<Question>)session.getAttribute("QUESTIONS");
			for(Question q:form.getQuestions()){
				addedQuestions.add(q);
			}
			
			
			// Removing attributes from the session
			session.removeAttribute("USER");
			session.removeAttribute("SUBJECTS");
			session.removeAttribute("ONGOING_SURVEY");
			session.removeAttribute("QUESTIONS");
			
			boolean status = evaluationService.submitSurveyResult(userObject, addedQuestions);
			
			if(status){
			return new ModelAndView("surveyComplete");}
			
			else {
				return new ModelAndView("error");
			}
		}
				
		else return new ModelAndView("error");
		
		
		
		
	}*/
	
	
	
	
	@RequestMapping(value="/survey/{cedulla}")
	public ModelAndView survey(@ModelAttribute("form") SurveyForm form,@PathVariable("cedulla") String cedulla,HttpSession session,ModelMap modelMap){
		
		System.out.println("Starting survey of "+cedulla);
		
		
		User user =null;
		Set<Subject> subjectsToAdd = null;
		
		if(form.getQuestions()==null||form.getQuestions().isEmpty()){
			
			user = evaluationService.getUserBasedOnCedulla(Integer.parseInt(cedulla));			
			subjectsToAdd = evaluationService.getSubjects(user);
			
			
			// Setting attributes in the session
			session.setAttribute("USER",user);
			
			return new ModelAndView("surveyForm","form",createForm(form,user,subjectsToAdd));
		}
		
		else{
			
			User userObject = (User)session.getAttribute("USER");
			
			// Removing attributes from the session
			session.removeAttribute("USER");
			
			boolean status = evaluationService.submitSurveyResult(userObject, form.getQuestions());
			
			if(status){
			return new ModelAndView("surveyComplete");}
			
			else {
				return new ModelAndView("error");
			}
	

					
		}
			
			
		
	
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	@RequestMapping(value="/cancelSurvey")
	public String cancelSurvey(HttpSession session){
		
		session.removeAttribute("USER");
		session.removeAttribute("SUBJECTS");
		session.removeAttribute("ONGOING_SURVEY");
		session.removeAttribute("QUESTIONS");
		
		return "/";
	}
	
	
	
	
	private SurveyForm createForm(SurveyForm form,User user,Set<Subject> subjects){
		form.setUser(user);
		List<Subject> result = new ArrayList<Subject>();
		for(Subject sub:subjects){
			result.add(sub);
		}
		form.setSubjects(result);
		form.setQuestions(new ArrayList<Question>());
		
		return form;
	}

}
